package com.cos.lession56.web.filter;

import com.cos.lession56.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: COS
 * @time: 2022/3/10 16:26
 * @description: 过滤器依赖于Servlet 几乎所有请求都可以过滤,css js 图片,
 * <p>
 * 拦截器
 * 底层是使用java的反射机制来完成,拦截可以做到一些细节的问题,单独拦截某一些方法,其他的不管
 * <p>
 * 过滤器可以过滤所有资源,包括controller html css js
 * 拦截器可以拦截controller的方法
 */
@WebFilter(filterName = "Filter01_Role",value = "/*")
public class Filter01_Role implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//        HttpServletRequest request= (HttpServletRequest) req;
//        HttpServletResponse response = (HttpServletResponse) resp;
//        //登录的设置
//        User is_login = (User) request.getSession().getAttribute("is_login");
//        if (is_login != null) {
//            //放行
//            chain.doFilter(request, response);
//        }
//        response.sendRedirect("/login.jsp");
        System.out.println("过滤器进去前");
        chain.doFilter(req, resp);
        System.out.println("过滤器回来");
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
